package dobest.ray.com.jiongjiong.activity.mumingleshi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseActivity;
import dobest.ray.com.jiongjiong.utils.MTextViewUtils;
import dobest.ray.com.jiongjiong.views.MTextView;
import dobest.ray.com.jiongjiong.views.MumingleshiNoticeDialog;
import dobest.ray.com.jiongjiong.views.NavigationBar;

/**
 * Created by ray on 2016/3/10.
 */
public class MuMingLeShiActivityWelcomeActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager mu_viewpager;
    // 底部园点
    private ImageView mpage0, mpage1;
    private RelativeLayout rl_dot_bg;
    private MumingleshiNoticeDialog dialog;
    private MTextView mtv_pager2_content;
    private Button btn_goto_test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mumingleshi_welcome);
        findViews();
        init();
        addListeners();
    }

    @Override
    public void findViews() {
        navi = (NavigationBar) findViewById(R.id.navi);
        navi.setTitle(getResources().getString(R.string.mumingleshi_title));
        navi.setBackListener(this, R.mipmap.img_back);
        mu_viewpager = (ViewPager) findViewById(R.id.mu_viewpager);
        mpage0 = (ImageView) findViewById(R.id.page0);
        mpage1 = (ImageView) findViewById(R.id.page1);
        rl_dot_bg = (RelativeLayout) findViewById(R.id.rl_dot_bg);

    }

    @Override
    public void init() {
        // 引导页
        LayoutInflater inflater = LayoutInflater.from(this);
        View view1 = inflater.inflate(R.layout.welcome_pager_one, null);
        View view2 = inflater.inflate(R.layout.welcome_pager_two, null);
        mtv_pager2_content = (MTextView) view2.findViewById(R.id.mtv_pager2_content);
        btn_goto_test = (Button) view2.findViewById(R.id.btn_goto_test);
        MTextViewUtils.setText(mtv_pager2_content, getResources().getString(R.string.mumingleshi_notice), this);


        final ArrayList<View> views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);

        PagerAdapter mPagerAdapter = new PagerAdapter() {

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                return views.size();
            }

            @Override
            public void destroyItem(View container, int position, Object object) {
                ((ViewPager) container).removeView(views.get(position));
            }

            @Override
            public Object instantiateItem(View container, int position) {
                ((ViewPager) container).addView(views.get(position));
                return views.get(position);
            }

        };

        mu_viewpager.setAdapter(mPagerAdapter);
        dialog = new MumingleshiNoticeDialog(this);
        dialog.show();

    }

    @Override
    public void addListeners() {
        mu_viewpager.setOnPageChangeListener(new MyOnPagechangeListener());
        btn_goto_test.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back_operate:
                finish();
                break;
            case R.id.btn_goto_test:
                Intent it = new Intent(this, MumingleshiActivity.class);
                startActivity(it);
                break;

        }
    }

    /**
     * ViewPager页面滑动监听
     */
    public class MyOnPagechangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    rl_dot_bg.setVisibility(View.VISIBLE);
                    mpage0.setImageDrawable(getResources().getDrawable(R.mipmap.dot_focused));
                    mpage1.setImageDrawable(getResources().getDrawable(R.mipmap.dot_normal));
                    break;
                case 1:
                    rl_dot_bg.setVisibility(View.INVISIBLE);
                    mpage0.setImageDrawable(getResources().getDrawable(R.mipmap.dot_normal));
                    mpage1.setImageDrawable(getResources().getDrawable(R.mipmap.dot_focused));
                    break;
            }

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

    }
}
